package com.oocl.crm;

import java.util.Arrays;

import com.oocl.crm.DoubleLinkList.Node;

public class Command {
	static String commadType(String command)
	{
		String[] type = command.split(" ");
		return type[0];
	}
	
	static void searchWithoutParameter(DoubleLinkList<Person> dll){
		DLLControl.printList(dll);
	}
	
	static void searchWithSingleParameter(DoubleLinkList<Person> dll,String command)
	{
		String[] sp1 = command.split(" ");
		String[] sp2 = sp1[1].split(":");
		DoubleLinkList<Person>.Node<Person> node ;
		switch (sp2[0]) {
		case "id":
			node = DLLControl.searchById(dll, sp2[1]);
			if(node!=null)DLLControl.printNode(node);
			break;
		case "name":
			node=dll.getHead();
			while(node.getNext()!=null)
			{
				if(DLLControl.compareByName(node.getNext(), sp2[1])){
					DLLControl.printNode(node.getNext());
				}
				node=node.getNext();
			}
			break;
		case "address":
			node=dll.getHead();
			while(node.getNext()!=null)
			{
				if(DLLControl.compareByAddress(node.getNext(), sp2[1])){
					DLLControl.printNode(node.getNext());
				}
				node=node.getNext();
			}
			break;
		case "sex":
			node=dll.getHead();
			while(node.getNext()!=null)
			{
				if(DLLControl.compareBySex(node.getNext(), sp2[1])){
					DLLControl.printNode(node.getNext());
				}
				node=node.getNext();
			}
			break;
		case "call":
			node=dll.getHead();
			while(node.getNext()!=null)
			{
				if(DLLControl.compareByCall(node.getNext(), sp2[1])){
					DLLControl.printNode(node.getNext());
				}
				node=node.getNext();
			}
			break;
		case "birthday":
			node=dll.getHead();
			while(node.getNext()!=null)
			{
				if(DLLControl.compareByBirthday(node.getNext(), sp2[1])){
					DLLControl.printNode(node.getNext());
				}
				node=node.getNext();
			}
			break;
		default:
			break;
		}
		
		
	}
	
	static void searchWithParameters(DoubleLinkList<Person> dll,String command)
	{
		String[] sp1=command.split(" ");
		String[] sp2=sp1[1].split(",");
		String[] sp3;
		DoubleLinkList<Person>.Node<Person> node;
		node=dll.getHead();
		boolean flag;
		while(node.getNext()!=null)
		{
			flag=true;
			for (int i = 0; i < sp2.length; i++) {
				sp3=sp2[i].split(":");
				switch (sp3[0]) {
				case "id":
					if(node.next.getData().id.compareTo(sp3[1])!=0)flag=false;
					break;
				case "name":
					if(node.next.getData().name.compareTo(sp3[1])!=0)flag=false;
					break;
				case "address":
					if(node.next.getData().address.compareTo(sp3[1])!=0)flag=false;
					break;
				case "sex":
					if(node.next.getData().sex.compareTo(sp3[1])!=0)flag=false;
					break;
				case "call":
					if(node.next.getData().call.compareTo(sp3[1])!=0)flag=false;
					break;
				case "birthday":
					if(node.next.getData().birthday.compareTo(sp3[1])!=0)flag=false;
					break;
				default:
					break;
				}
			}
			if(flag){
				DLLControl.printNode(node.getNext());
			}
			node=node.getNext();
		}
	}
	
	static void addPerson(DoubleLinkList<Person> dll,String command)
	{
		Person person=new Person("null","null","null","null","null","null");
		String[] sp1 = command.split(" ");
		String[] sp2 = sp1[1].split(",");
		String[] sp3 ;
		if(dll.getSize()+1<10)person.id="0"+String.valueOf((dll.getSize()+1));
		else person.id=String.valueOf((dll.getSize()+1));
		int l=sp2.length;
		for (int i = 0; i < l; i++) {
			sp3=sp2[i].split(":");
			addOrUpdateInformation(person,sp3);
		}
		if(person.name=="null")System.out.println("The user's name can not be null!");
		else dll.addNode(person);
	}
	
	static void addOrUpdateInformation(Person person,String[] inf)
	{
		switch (inf[0]) {
		case "name":
			person.name=inf[1];
			break;
		case "sex":
			person.sex=inf[1];
			break;
		case "birthday":
			person.birthday=inf[1];
			break;
		case "address":
			person.address=inf[1];
			break;
		case "call":
			person.call=inf[1];
			break;
		default:
			break;
		}
	}
	
	static void deletePersonById(DoubleLinkList<Person> dll,String command)
	{
		DoubleLinkList<Person>.Node<Person> node;
		String[] str;
		str=command.split(" ");
		node=DLLControl.searchById(dll, str[1]);
		if(node!=null)DLLControl.deleteNode(dll, node);
		else System.out.println("Id "+str[1]+" "+"does not exist.");
	}
	
	static void updatePerson(DoubleLinkList<Person> dll,String command)
	{
		Person person;
		DoubleLinkList<Person>.Node<Person> node;
		String[] sp1=command.split(" ");
		String[] inf=sp1[2].split(",");
		String id = sp1[1];
		String[] infs;
		node=DLLControl.searchById(dll, id);
		if(node!=null){
			person=node.getData();
			for (int i = 0; i < inf.length; i++) {
				infs=inf[i].split(":");
				addOrUpdateInformation(person, infs);
			}
			node.setData(person);
		}
		else System.out.println("Id "+id+" "+"does not exist.");
	}

	static void sortAndPrint(DoubleLinkList<Person> dll){
		DoubleLinkList<Person> viewdll=new DoubleLinkList<Person>();
		DoubleLinkList<Person>.Node<Person> node=dll.getHead();
		for (int i = 0; i < dll.getSize(); i++) {
			viewdll.addNode(node.getNext().getData());
			node=node.getNext();
		}
		DLLControl.bubbleSortById(viewdll);
		searchWithoutParameter(viewdll);
	}
	
	static void helpWithoutParameter(){

		System.out.printf("%-20s%-40s%-25s\n","Command","Usage","Manual");
		System.out.printf("%-20s%-40s%-25s\n","L","L name:XXX,sex:XXX...","Search user information by parameters");
		System.out.printf("%-20s%-40s%-25s\n","","L","Print the whole user information list");
		System.out.printf("%-20s%-40s%-25s\n","A","A name:XXX,sex:XXX...","Add user information");
		System.out.printf("%-20s%-40s%-25s\n","D","D XXX","Delete user information by ID");
		System.out.printf("%-20s%-40s%-25s\n","U","U XXX name:XXX,sex:XXX","Update user information by ID");
		System.out.printf("%-20s%-40s%-25s\n","O","O","Sort and print the whole user information list");
		System.out.printf("%-20s%-40s%-25s\n","H","H XXX","Show XXX help information");
		System.out.printf("%-20s%-40s%-25s\n","","H","Show help information of all the commands");
		System.out.printf("%-20s%-40s%-25s\n\n","Q","Q","Quit");
	}
	static void helpWithParameter(String command){
		String[] str = command.split(" ");
		try {
			switch (str[1]) {
			case "L":
				System.out.printf("%-20s%-40s%-25s\n", "Command", "Usage",
						"Manual");
				System.out.printf("%-20s%-40s%-25s\n", "L",
						"L name:XXX,sex:XXX...",
						"Search user information by parameters");
				System.out.printf("%-20s%-40s%-25s\n\n", "", "L",
						"Print the whole user information list");
				break;
			case "A":

				System.out.printf("%-20s%-40s%-25s\n", "Command", "Usage",
						"Manual");
				System.out.printf("%-20s%-40s%-25s\n\n", "A",
						"A name:XXX,sex:XXX...", "Add user information");
				break;
			case "D":
				System.out.printf("%-20s%-40s%-25s\n", "Command", "Usage",
						"Manual");
				System.out.printf("%-20s%-40s%-25s\n\n", "D", "D XXX",
						"Delete user information by ID");
				break;
			case "U":
				System.out.printf("%-20s%-40s%-25s\n", "Command", "Usage",
						"Manual");
				System.out.printf("%-20s%-40s%-25s\n\n", "U",
						"U XXX name:XXX,sex:XXX",
						"Update user information by ID");
				break;
			case "O":
				System.out.printf("%-20s%-40s%-25s\n", "Command", "Usage",
						"Manual");
				System.out.printf("%-20s%-40s%-25s\n\n", "O", "O",
						"Sort and print the whole user information list");
				break;
			case "H":
				System.out.printf("%-20s%-40s%-25s\n", "Command", "Usage",
						"Manual");
				System.out.printf("%-20s%-40s%-25s\n", "H", "H XXX",
						"Show XXX help information");
				System.out.printf("%-20s%-40s%-25s\n\n", "", "H",
						"Show help information of all the commands");
				break;
			case "Q":
				System.out.printf("%-20s%-40s%-25s\n", "Command", "Usage",
						"Manual");
				System.out.printf("%-20s%-40s%-25s\n\n", "Q", "Q", "Quit");
				break;
			default:
				break;
			}
		} catch (Exception e) {
			System.out.println("Error command!");
			// TODO: handle exception
		}
	}
}	
